<template>
	<div>
		<el-drawer title="" :visible.sync="drawer" :direction="direction" :before-close="handleClose" custom-class="drawer-info">
			<div class="info-item">
				<h5 class="from-title">查看评论</h5>
				<el-empty description="暂无评论" v-if="evaluateList.length == 0"></el-empty>
				<div class="item" v-for="item in evaluateList" :key="item.id">
					<div class="header">
						<img class="image" :src="item.avatar" alt="" />
						<div class="info">
							<div class="delete">
								<div class="name">{{ item.nickName }}</div>
								<i class="el-icon-delete del" @click="deleteComment(item.id)"></i>
							</div>
							<div class="time">
								{{ item.commentTime }}
							</div>
						</div>
					</div>
					<div class="message">{{ item.content }}</div>
					<div class="reply">
						<div class="reply-item" v-if="item.children">
							<div v-show="item.children.length >= 1" class="fistItem">
								<div class="fistItem-left">
									<span class="label">{{ item.children.length ? item.children[0].nickName : "" }}:</span>
									{{ item.children.length ? item.children[0].content : "" }}
								</div>
								<div class="fistItem-right">
									<i class="el-icon-delete del" style="margin-left: auto;" @click="deleteComment(item.children[0].id)"></i>
								</div>
							</div>
							<div v-for="child in replyChilden" :key="child.id" class="child-item">
								<div v-show="item.replyVisible && item.id == child.parentId" class="fistItem">
									<div class="fistItem-left">
										<span class="label">{{ child.nickName }}:</span>
										{{ child.content }}
									</div>
									<div class="fistItem-right">
										<i class="el-icon-delete del" style="margin-left: auto;" @click="deleteComment(child.id, 3)"></i>
									</div>
								</div>
							</div>
						</div>
						<div class="unfold" v-show="item.children && item.children.length > 1" @click="checkMore(item)">{{ item.replyVisible ? "收起" : "展开" }}</div>
					</div>
				</div>
				<pagination v-if="page.total > 0" :total="page.total" :current-page="page.current" :page-size="page.size" @refreshData="init(ids)"></pagination>
			</div>
		</el-drawer>
	</div>
</template>
<script>
import { studyCourseList, studyCourseReply, studyCourseDeleteComment } from "@/api/study/course/studycourse.js"
import FortressPagination from "@/components/fortress-pagination.vue"
export default {
	data() {
		return {
			drawer: true,
			direction: "rtl",
			evaluateList: [],
			replyChilden: [],
			replyVisible: false,
			page: {
				total: 0,
				current: 1,
				size: 10,
			},
			ids: "",
		}
	},
	created() {},
	methods: {
		init(id) {
			this.ids = id
			if (id) {
				studyCourseList({
					commentType: "00",
					parentId: id,
					current: this.page.current,
					size: this.page.size,
				}).then(res => {
					this.evaluateList = res.data.records
					this.evaluateList.forEach(item => {
						item.replyVisible = this.replyVisible
					})
					this.page.total = res.data.total
				})
			}
			this.drawer = true
		},
		checkMore(item) {
			this.evaluateList.forEach(evaluate => {
				if (evaluate.id == item.id) {
					evaluate.replyVisible = !evaluate.replyVisible
				} else {
					evaluate.replyVisible = false
				}
			})
			studyCourseReply({ parentId: item.id }).then(res => {
				res.data.forEach(itemR => {
					this.replyChilden = itemR.children.slice(1)
				})
			})
		},
		// 关闭抽屉
		handleClose(done) {
			done()
		},
		deleteComment(id, flag) {
			this.$confirm("删除后不可恢复，您确定要删除吗？", "删除确认", {
				confirmButtonText: "确定",
				cancelButtonText: "取消",
				type: "warning",
			}).then(() => {
				studyCourseDeleteComment(id).then(() => {
					this.$message({
						type: "success",
						message: "删除成功!",
					})
					if (flag === 3) {
						let index = this.replyChilden.findIndex(item => item.id == id)
						this.replyChilden.splice(index, 1)
					} else {
						this.init(this.ids)
					}
				})
			})
		},
	},
	components: {
		pagination: FortressPagination,
	},
	watch: {},
	computed: {},
}
</script>
<style scoped lang="scss">
.from-title {
	margin-bottom: 20px;
	padding: 0 20px 20px 20px;
	font-size: 16px;
	color: #17233d;
	border-bottom: 1px solid #eee;
}
.info-item {
	margin-bottom: 40px;
}

.form-item {
	margin-bottom: 20px;
	font-size: 14px;
}
.form-item-label {
	text-align: right;
	float: left;
	width: 25%;
}
.form-item-content {
	margin-left: 30%;
	width: 70%;
}
.item {
	padding: 10px 20px 10px;
	margin-bottom: 15px;
	.header {
		display: flex;
		justify-content: flex-start;
		align-items: flex-start;
		.image {
			width: 42px;
			height: 42px;
			border-radius: 50%;
			display: block;
			margin-right: 8px;
			flex: none;
		}
		.info {
			flex: 1;
			.delete {
				display: flex;
				justify-content: space-between;
				align-items: center;

				.name {
					font-size: 14px;
					padding-top: 5px;
					padding-bottom: 5px;
				}
				.del {
					cursor: pointer;
					font-size: 14px;
				}
			}
			.time {
				color: #acacac;
				font-size: 12px;
				display: flex;
				justify-content: flex-start;
				align-items: center;
				.kudos {
					margin-left: auto;
					.icon {
						width: 14px;
						height: 13px;
						margin-left: 2px;
					}
				}
			}
		}
	}

	.message {
		padding-left: 50px;
		color: #454545;
		font-size: 14px;
		padding-top: 8px;
		padding-bottom: 10px;
	}
	.reply {
		padding-bottom: 13px;
		position: relative;
		border-bottom: 1px solid #f5f5f5;
		margin-left: 50px;
		color: #454545;
		padding-left: 12px;
		font-size: 14px;
		&::before {
			content: "";
			position: absolute;
			top: 0;
			left: 0;
			width: 2px;
			height: 25px;
			background: #f9f9f9;
		}
		.label {
			color: #3c9cff;
			margin-right: 5px;
		}
		.child-item {
			margin-bottom: 5px;
			&:last-child {
				margin-bottom: 0;
			}
		}

		.fistItem {
			display: flex;
			justify-content: space-between;
			margin-bottom: 4px;
			&:last-child {
				margin-bottom: 0;
			}
			.fistItem-right {
				cursor: pointer;
			}
		}
	}
	.unfold {
		cursor: pointer;
		color: #3c9cff;
		font-size: 13px;
		margin-top: 9px;
		display: flex;
		justify-content: flex-start;
		align-items: center;
		gap: 5px;
		&::before {
			content: "";
			display: inline-block;
			width: 1em;
			height: 1em;
			border-bottom: 1px solid #3c9cff;
			border-left: 1px solid #3c9cff;
			transform: rotate(-45deg) scale(0.5);
		}
	}
	.unfold-up {
		color: #3c9cff;
		font-size: 13px;
		margin-top: 9px;
		display: flex;
		justify-content: flex-start;
		align-items: center;
		gap: 5px;
		&::before {
			content: "";
			display: inline-block;
			width: 1em;
			height: 1em;
			border-bottom: 1px solid #3c9cff;
			border-left: 1px solid #3c9cff;
			transform: rotate(135deg) scale(0.5);
		}
	}
}
</style>
